const axios = require('axios')

/**
 * 获取教程分类
 * @returns
 */
exports.apiGetTutorialCategory = function () {
  return new Promise((resolve, reject) => {
    axios.post('https://www.developers.pub/developers-server/rest/wiki/category/all').then(res => {
      if (res.data.code === 0) {
        resolve(res.data.data)
      } else {
        reject(res)
      }
    }).catch(reject)
  })
}

/**
 * 根据教程分类获取教程列表
 * @param {*} categoryId
 * @returns
 */
exports.apiGetTutorialList = function (categoryId) {
  return new Promise((resolve, reject) => {
    const searchParams = {
      pageNo: 1,
      pageSize: 50,
      filter: {
        categoryId
      }
    }
    axios.post('https://www.developers.pub/developers-server/rest/wiki/list', searchParams).then(res => {
      if (res.data.code === 0) {
        resolve(res.data.data)
      } else {
        reject(res)
      }
    }).catch(reject)
  })
}

/**
 * 获取指定教程目录信息
 * @param {*} tutorialUuid
 * @returns
 */
exports.apiGetTutorialDetail = function (tutorialId) {
  return new Promise((resolve, reject) => {
    axios.post(`https://www.developers.pub/developers-server/rest/wiki/info/${tutorialId}`).then(res => {
      if (res.data.code === 0) {
        resolve(res.data.data)
      } else {
        reject(res.data)
      }
    }).catch(reject)
  })
}

exports.apiGetTutorialChapterInfo = function (chapterUuid) {
  return new Promise((resolve, reject) => {
    axios.post(`https://www.developers.pub/developers-server/rest/posts/info/${chapterUuid}`).then(res => {
      if (res.data.code === 0) {
        resolve(res.data.data)
      } else {
        reject(res.data)
      }
    }).catch(reject)
  })
}

exports.apiGetTutorialChapterComment = function (chapterUuid) {
  return new Promise((resolve, reject) => {
    axios.post('https://www.developers.pub/developers-server/rest/comment/page', {
      filter: chapterUuid,
      pageNo: 1,
      pageSize: 20
    }).then(res => {
      if (res.data.code === 0) {
        resolve(res.data.data)
      } else {
        reject(res.data)
      }
    }).catch(reject)
  })
}
